<template>
  <div class="container">
    <p>吴进先</p>
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 搜索框 -->
          <el-col :span="6">
            <el-form ref="subjectFrom" :model="queryList" label-width="80px">
              <el-form-item label="学科名称">
                <el-input
                  v-model="queryList.subjectName"
                  style="height: 32px"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="18">
            <el-button class="colInput" plain @click="eliminate"
              >清除</el-button
            >
            <el-button class="colInput" type="primary" @click="search"
              >搜索</el-button
            >
          </el-col>
        </el-col>
        <el-col :span="6">
          <el-button
            class="colright"
            type="success"
            icon="el-icon-edit"
            @click="subject = true"
            >新增学科</el-button
          >
        </el-col>
      </el-row>
      <el-alert :title="'数据共' + total + '条'" type="info" show-icon>
      </el-alert>
      <!-- Table表格 -->
      <el-table :data="subjectsList" style="width: 100%">
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="username" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template v-slot="{ row }">
            {{ row.isFrontDisplay === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="text" size="small" @click="classification(row)"
              >学科分类</el-button
            >
            <el-button type="text" size="small" @click="JumpLabel(row)"
              >学科标签</el-button
            >
            <el-button type="text" size="small" @click="modify(row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="deleteDiscipline(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="tablePage.pagesize"
        :page-sizes="number"
        layout="prev, pager, next,sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 新增学科 -->
    <el-dialog
      :title="titleText"
      :visible.sync="subject"
      width="30%"
      @close="onClose"
    >
      <el-form ref="form" :rules="rules" :model="ruleForm" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="ruleForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="delivery"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="determineSubject">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
export default {
  name: 'subjects',
  data () {
    return {
      queryList: {
        subjectName: ''
      },
      delivery: false,
      ruleForm: {
        subjectName: '',
        isFrontDisplay: ''
      },
      addDate: '',
      rules: {
        subjectName: [
          { required: true, message: '学科名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      },
      subjectsList: [],
      tablePage: {
        page: 1,
        pagesize: 5
      },
      subject: false,
      total: 0,
      number: [5, 10, 20, 50]
    }
  },
  computed: {
    isFrontDisplay () {
      if (this.delivery) {
        return 1
      } else {
        return 0
      }
    },
    titleText () {
      return (this.ruleForm.id ? '修改' : '新增') + '学科'
    }
  },
  created () {
    this.loadDisciplines()
  },
  methods: {
    // 获取全部学科列表
    async loadDisciplines () {
      const { data: res } = await list({
        ...this.tablePage,
        subjectName: this.queryList.subjectName
      })
      this.total = res.counts
      res.items.forEach(item => {
        item.addDate = this.$dayjs(item.addDate).format('YYYY-MM-DD hh:mm:ss')
      })
      this.subjectsList = res.items
    },
    // 清除按钮
    eliminate () {
      if (this.queryList.subjectName === '') {
        this.queryList.subjectName = ''
      } else {
        this.queryList.subjectName = ''
        this.loadDisciplines()
      }
    },
    // 搜索
    search () {
      if (this.queryList.subjectName === '') {
        return ''
      } else {
        this.loadDisciplines()
      }
    },
    // 关闭弹框
    onClose () {
      this.$refs.form.resetFields()
      this.ruleForm = {
        subjectName: '',
        isFrontDisplay: ''
      }
      this.delivery = false
      this.subject = false
    },
    // 点击确定
    async determineSubject () {
      await this.$refs.form.validate()
      this.ruleForm.isFrontDisplay = this.isFrontDisplay
      try {
        if (this.ruleForm.id) {
          await update(this.ruleForm)
          this.$message.success('修改成功')
        } else {
          await add(this.ruleForm)
          this.$message.success('新增成功')
        }
        this.delivery = false
      } catch (err) {}
      this.loadDisciplines()
      this.subject = false
    },
    // 修改
    async modify (row) {
      this.subject = true
      this.ruleForm = row
      this.delivery = Boolean(row.isFrontDisplay)
    },
    // 删除学科
    async deleteDiscipline (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.$message.success('删除成功!')
          this.loadDisciplines()
        })
        .catch(() => {})
    },
    // 学科分类
    classification (row) {
      this.$router.push({
        path: 'directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 学科标签
    JumpLabel (row) {
      this.$router.push({
        path: 'tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 分页
    handleSizeChange (val) {
      this.tablePage.pagesize = val
      this.loadDisciplines()
    },
    handleCurrentChange (val) {
      this.tablePage.page = val
      this.loadDisciplines()
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  .colInput {
    height: 35px;
  }
  .colright {
    float: right;
    height: 35px;
  }
  .subspan {
    font-size: 14px;
    font-weight: 700;
    margin-right: 10px;
    margin-left: 10px;
  }
  .el-alert {
    margin-bottom: 20px;
  }
  .el-pagination {
    float: right;
    margin-top: 20px;
  }
}
</style>
